package com.demon.androidbasic.action

import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.IdRes

/**
 * 防双击监视器
 */
interface NoDoubleClickAction : View.OnClickListener {

    fun <V : View> findViewById(@IdRes id: Int): V

    fun onNoDoubleClick(v: View) {}

    var clickTime: Long

    @CallSuper
    override fun onClick(v: View) {
        val now = System.currentTimeMillis()
        if (now - clickTime > 1000) {
            clickTime = now
            onNoDoubleClick(v)
        }
    }

    fun setOnClickListener(@IdRes vararg ids: Int) {
        for (id in ids) {
            findViewById<View>(id).setOnClickListener(this)
        }
    }

    fun setOnClickListener(vararg views: View) {
        for (view in views) {
            view.setOnClickListener(this)
        }
    }

}